<?php

namespace frontend\modules\business\controllers;

use yii;
use yii\web\Controller;
use yii\data\Pagination;

/**
 * Default controller for the `business` module
 */
class BalanceController extends Controller
{
    public $business = [];
    
    public $master = null;
    
    public function init()
    {
        parent::init();
        
        $this->business = $this->module->params['business'];
        
        $this->enableCsrfValidation = false;

        $masterConfig = $this->module->params['master'];
        
        $this->master = new yii\db\Connection([
            'dsn' => "mysql:host={$masterConfig['host']};dbname={$masterConfig['dbname']}",
            'username' => $masterConfig['username'],
            'password' => $masterConfig['password'],
            'charset' => $masterConfig['charset'],
            'tablePrefix' => $masterConfig['tablePrefix'],
        ]);
        
    }
    
    //显示消费余额详情
    public function actionDetail()
    {
        
        $pageSize = 300;
        
        $admin_id = Yii::$app->request->get('admin_id');
        
        if (intval($admin_id) == 0) {
            exit("admin empty \n");
        }
        
        if (!array_key_exists($admin_id, $this->business)) {
            exit("admin config not setting \n");
        }
        
        //独立商户端额配置信息
        $business =  $this->business[$admin_id];
        $host = $business['host'];
        $dbname = isset($business['dbname']) ? $business['dbname'] : 'xddb';
        $username = isset($business['username']) ? $business['username'] : 'dladmin';
        $password = isset($business['password']) ? $business['password'] : 'root@123$';
        $startID = isset($business['startID']) ? $business['startID'] : -1;
        
        if ($startID == -1) {
            exit("not setting startid \n");
        }
        
        //查询独立商户的余额
        $db = new yii\db\Connection([
            'dsn' => "mysql:host=$host;dbname=$dbname",
            'username' => "$username",
            'password' => "$password",
            'charset' => 'utf8',
            'tablePrefix' => 'busy_'
        ]);
        
        $sql_total = "select count(*) as count from busy_expense where id >= :id and userid =:admin_id";
        
        $duli_count = $db->createCommand($sql_total)
        ->bindValue(':id', $startID)
        ->bindValue(':admin_id', $admin_id)
        ->queryOne();
        
        
        $master_count = $this->master->createCommand($sql_total)
        ->bindValue(':id', $startID)
        ->bindValue(':admin_id', $admin_id)
        ->queryOne();
        
        $pagination = new Pagination(['totalCount' => $duli_count['count'],'defaultPageSize'=>$pageSize,'pageSizeLimit'=>false]);

        //独立数据
        $duli_data_sql = "select * from busy_expense where id >= :id order by id asc limit :offset , :limit ";
        
        $duli_data = $db->createCommand($duli_data_sql)
        ->bindValue(':id', $startID)
        ->bindValue(':offset', $pagination->offset)
        ->bindValue(':limit', $pagination->limit)
        ->queryAll();
        
        
        //主数据
        $master_data_sql = "select * from busy_expense where id >= :id and userid = :admin_id order by id asc limit :offset , :limit ";
        
        $master_data = $this->master->createCommand($master_data_sql)
        ->bindValue(':id', $startID)
        ->bindValue(':admin_id', $admin_id)
        ->bindValue(':offset', $pagination->offset)
        ->bindValue(':limit', $pagination->limit)
        ->queryAll();
        
        
        
        return $this->render('detail',[
            'duli'=>['data'=>$duli_data,'total'=>$duli_count],
            'master'=>['data'=>$master_data,'total'=>$master_count],
            'pagination'=>$pagination,
        ]);
        
    }
    

}
